package com.example.drawing.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import com.example.drawing.R;
import com.example.drawing.view.MobileView;

public class Activity_3_Mobile_2 extends AppCompatActivity {
    private MobileView mobileView;
    private long lastTimeStamp;
private boolean isPress;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_3_mobile);
//        setContentView(new MobileView(this));


        mobileView=(MobileView)findViewById(R.id.mobile_view);
        //mobileView = new MobileView(Activity_3_Mobile.this);


        Button middle = (Button) findViewById(R.id.middle);
        Button top = (Button) findViewById(R.id.top);
        Button bottom = (Button) findViewById(R.id.bottom);
        Button left = (Button) findViewById(R.id.left);
        Button right = (Button) findViewById(R.id.right);

        top.setOnTouchListener(new BtnTouchListener(0, -50));

        bottom.setOnTouchListener(new BtnTouchListener(0, 50));

        left.setOnTouchListener(new BtnTouchListener(-50,0));

        right.setOnTouchListener(new BtnTouchListener(50,0));



//        top.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                switch (event.getAction()) {
//                    case MotionEvent.ACTION_DOWN:
//                        Log.e("xxx","按下");
//
//                        isPress=true;
//                        lastTimeStamp=System.currentTimeMillis();
//                        new Thread(new Runnable() {
//                            @Override
//                            public void run() {
//                                while (isPress){
//                                    //判断按压时间超过300毫秒视为长按
//                                    if((System.currentTimeMillis()-lastTimeStamp)>300){
//
//                                        try {
//                                            Thread.sleep(50);//间隔50毫秒这里决定长按时 执行点击方法的频率
//                                        } catch (InterruptedException e) {
//                                            e.printStackTrace();
//                                        }
//                                    }
//                                }
//                            }
//                        }).start();
//                        break;
//                    case MotionEvent.ACTION_UP:
//                        Log.e("xxx","抬起");
//                        break;
//                    case MotionEvent.ACTION_MOVE:
//                        Log.e("xxx","移动");
//                        break;
//                }
//                return true;
//
//            }
//        });





    }
    class BtnTouchListener implements View.OnTouchListener {
        int x,y;

        public BtnTouchListener(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        Log.e("xxx", "按下");
                        isPress=true;
                        lastTimeStamp=System.currentTimeMillis();
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                while (isPress){
                                    //判断按压时间超过300毫秒视为长按
                                    if((System.currentTimeMillis()-lastTimeStamp)>300){
                                        try {
                                            Thread.sleep(50);//间隔50毫秒这里决定长按时 执行点击方法的频率
                                        } catch (InterruptedException e) {
                                            e.printStackTrace();
                                        }
                                        mobileView.mibile(x, y);
                                    }
                                }
                            }
                        }).start();

                        break;
                    case MotionEvent.ACTION_UP:
                        Log.e("xxx", "抬起");
                        if (System.currentTimeMillis()-lastTimeStamp<300){
                            mobileView.mibile(x, y);
                        }
                        isPress=false;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        Log.e("xxx", "移动");
                        break;
                }
                System.out.println(lastTimeStamp);
                return true;

            }
        }
    }

    //定义一个内部类,实现View.OnClickListener接口,并重写onClick()方法
    class BtnClickListener implements View.OnClickListener
    {
        int x,y;

        public BtnClickListener(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public BtnClickListener() {
        }

        @Override
        public void onClick(View v) {
            mobileView.mibile(x, y);
        }
    }
}